package ngames.com.sushitv.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.activity.AnimeIntroActivity;
import ngames.com.sushitv.activity.FanMadeIntroActivity;
import ngames.com.sushitv.base.BaseListAdapter;
import ngames.com.sushitv.entity.Anime;
import ngames.com.sushitv.entity.ItemVideo;
import ngames.com.sushitv.entity.SearchRequestAll;
import ngames.com.sushitv.entity.Tag;

/**
 * Created by Jeffrey on 2016/7/27.
 */
public class SearchRequestAdapter extends BaseListAdapter<ItemVideo> {
    private SearchRequestAll searchRequestAll;
    private static final int TYPE_TAG = 2;
    private static final int TYPE_ANIME = 1;
    //private static final int TYPE_SCREEN = 0;
    private List<Anime> anime;
    private List<Tag> tag;
    private int animeSize;
    private int tagSize;

    public SearchRequestAdapter(Context context, SearchRequestAll requestAll) {
        super(context, null);
        this.searchRequestAll = requestAll;
        anime = searchRequestAll.getAnimes();
        tag = searchRequestAll.getTags();
        animeSize = anime == null ? 0 : anime.size();
        tagSize = tag == null ? 0 : tag.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_ANIME) {
            return new AnimeViewHolder(mInflater.inflate(R.layout.item_anime, parent, false));
        } else {
            return new TagViewHolder(mInflater.inflate(R.layout.item_fanmade, parent, false));
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       if (position < animeSize ) {
            AnimeViewHolder animeViewHolder = (AnimeViewHolder) holder;
            animeViewHolder.setData(position);
        } else {
            TagViewHolder tagViewHolder = (TagViewHolder) holder;
            tagViewHolder.setData(position);
        }
    }

    @Override
    public int getItemCount() {
        return animeSize + tagSize;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < animeSize) {
            return TYPE_ANIME;
        } else {
            return TYPE_TAG;
        }
    }

    class AnimeViewHolder extends RecyclerView.ViewHolder {
        TextView titleTxt, watchedTxt, updatedTxt;
        ImageView coverImg;
        RelativeLayout itemLayout;
        public AnimeViewHolder(View itemView) {
            super(itemView);
            itemLayout= (RelativeLayout) itemView.findViewById(R.id.relative_item_anime);
            titleTxt = (TextView) itemView.findViewById(R.id.titleText);
            coverImg = (ImageView) itemView.findViewById(R.id.cover);
            watchedTxt = (TextView) itemView.findViewById(R.id.watchedText);
            updatedTxt = (TextView) itemView.findViewById(R.id.updatedText);
        }

        public void setData(int position) {
            final Anime a = anime.get(position);
            titleTxt.setText(a.getTitle());
            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    /*.showStubImage(R.drawable.image_is_loading)            // 设置图片下载期间显示的图片
                    .showImageForEmptyUri(R.drawable.image_loading_failed)    // 设置图片Uri为空或是错误的时候显示的图片
                    .showImageOnFail(R.drawable.image_loading_failed)        // 设置图片加载或解码过程中发生错误显示的图片
                   */.cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                    .cacheOnDisc(true)                            // 设置下载的图片是否缓存在SD卡中
                    .build();
            String workerImgStr = a.getCover();
            ImageLoader.getInstance().displayImage(workerImgStr, coverImg, options);// 创建配置过得DisplayImageOption对象

            watchedTxt.setText(a.getSummary());
            updatedTxt.setText("Updated to EP " + a.getLast_episode());
            itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppApplication.setmVsId(a.getV_id()+"");
                    Intent intent=new Intent(mContext, AnimeIntroActivity.class);
                    mContext.startActivity(intent);
                }
            });
        }
    }

    class TagViewHolder extends RecyclerView.ViewHolder {
        TextView titleTxt, playCountText, readCountText, uploaderText;
        ImageView coverImg;
        RelativeLayout itemLayout;
        public TagViewHolder(View itemView) {
            super(itemView);
            itemLayout= (RelativeLayout) itemView.findViewById(R.id.relative_item_fanmade);
            titleTxt = (TextView) itemView.findViewById(R.id.titleText);
            playCountText = (TextView) itemView.findViewById(R.id.playCountText);
            readCountText = (TextView) itemView.findViewById(R.id.readCountText);
            coverImg = (ImageView) itemView.findViewById(R.id.cover);
            uploaderText = (TextView) itemView.findViewById(R.id.uploader);
        }

        public void setData(int position) {
            final Tag t = tag.get(position - animeSize);
            titleTxt.setText(t.getTitle());
            playCountText.setText(t.getPlay());
            readCountText.setText(t.getDanmu());
            uploaderText.setText("Uploader: " + t.getUper());
            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    /*.showStubImage(R.drawable.image_is_loading)            // 设置图片下载期间显示的图片
                    .showImageForEmptyUri(R.drawable.image_loading_failed)    // 设置图片Uri为空或是错误的时候显示的图片
                    .showImageOnFail(R.drawable.image_loading_failed)        // 设置图片加载或解码过程中发生错误显示的图片
                   */.cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                    .cacheOnDisc(true)                            // 设置下载的图片是否缓存在SD卡中
                    .build();
            String workerImgStr = t.getCover();
            ImageLoader.getInstance().displayImage(workerImgStr, coverImg, options);// 创建配置过得DisplayImageOption对象
            itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //String id=t.getVs_id();
                    AppApplication.setmVsId(t.getVs_id());
                    Intent intent = new Intent(mContext, FanMadeIntroActivity.class);
                   /* Bundle data = new Bundle();
                    data.putString("vs_id",t.getVs_id());
                    intent.putExtras(data);*/
                    mContext.startActivity(intent);

                    //Toast.makeText(mContext,"hahaha",Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
